<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>进度显示测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .test-section {
            background: white;
            padding: 20px;
            margin: 20px 0;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .progress-bar {
            width: 100%;
            height: 20px;
            background: #e0e0e0;
            border-radius: 10px;
            overflow: hidden;
            margin: 10px 0;
        }
        .progress-fill {
            height: 100%;
            background: #4CAF50;
            transition: width 0.3s ease;
        }
        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin: 10px 0;
        }
        .info-item {
            background: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            border-left: 4px solid #007bff;
        }
        .info-label {
            font-weight: bold;
            color: #495057;
            font-size: 12px;
        }
        .info-value {
            color: #212529;
            font-size: 14px;
            margin-top: 4px;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .log {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 10px;
            max-height: 300px;
            overflow-y: auto;
            font-family: monospace;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <h1>M3U8下载器进度显示测试</h1>
    
    <div class="test-section">
        <h2>模拟任务进度</h2>
        <div class="info-grid">
            <div class="info-item">
                <div class="info-label">任务状态</div>
                <div class="info-value" id="status">等待中</div>
            </div>
            <div class="info-item">
                <div class="info-label">进度百分比</div>
                <div class="info-value" id="progress">0%</div>
            </div>
            <div class="info-item">
                <div class="info-label">已下载大小</div>
                <div class="info-value" id="downloadedSize">0 B</div>
            </div>
            <div class="info-item">
                <div class="info-label">总文件大小</div>
                <div class="info-value" id="totalSize">0 B</div>
            </div>
            <div class="info-item">
                <div class="info-label">已下载分片</div>
                <div class="info-value" id="downloadedSegments">0</div>
            </div>
            <div class="info-item">
                <div class="info-label">总分片数</div>
                <div class="info-value" id="totalSegments">0</div>
            </div>
        </div>
        
        <div class="progress-bar">
            <div class="progress-fill" id="progressFill" style="width: 0%"></div>
        </div>
        
        <button onclick="startSimulation()">开始模拟下载</button>
        <button onclick="pauseSimulation()">暂停</button>
        <button onclick="resumeSimulation()">继续</button>
        <button onclick="resetSimulation()">重置</button>
    </div>
    
    <div class="test-section">
        <h2>控制台日志</h2>
        <div class="log" id="log"></div>
        <button onclick="clearLog()">清空日志</button>
    </div>

    <script>
        let simulationInterval;
        let isPaused = false;
        let currentProgress = 0;
        let downloadedSize = 0;
        let totalSize = 100 * 1024 * 1024; // 100MB
        let downloadedSegments = 0;
        let totalSegments = 100;
        
        function log(message) {
            const logDiv = document.getElementById('log');
            const timestamp = new Date().toLocaleTimeString();
            logDiv.innerHTML += `[${timestamp}] ${message}\n`;
            logDiv.scrollTop = logDiv.scrollHeight;
            console.log(message);
        }
        
        function updateDisplay() {
            document.getElementById('status').textContent = isPaused ? '已暂停' : '下载中';
            document.getElementById('progress').textContent = currentProgress + '%';
            document.getElementById('downloadedSize').textContent = formatFileSize(downloadedSize);
            document.getElementById('totalSize').textContent = formatFileSize(totalSize);
            document.getElementById('downloadedSegments').textContent = downloadedSegments;
            document.getElementById('totalSegments').textContent = totalSegments;
            document.getElementById('progressFill').style.width = currentProgress + '%';
        }
        
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 B';
            const k = 1024;
            const sizes = ['B', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
        
        function startSimulation() {
            if (simulationInterval) {
                clearInterval(simulationInterval);
            }
            
            isPaused = false;
            log('开始模拟下载...');
            
            simulationInterval = setInterval(() => {
                if (!isPaused) {
                    currentProgress += 1;
                    downloadedSize = Math.floor((totalSize * currentProgress) / 100);
                    downloadedSegments = Math.floor((totalSegments * currentProgress) / 100);
                    
                    if (currentProgress >= 100) {
                        currentProgress = 100;
                        downloadedSize = totalSize;
                        downloadedSegments = totalSegments;
                        clearInterval(simulationInterval);
                        log('下载完成！');
                    }
                    
                    updateDisplay();
                    log(`进度更新: ${currentProgress}%, 大小: ${formatFileSize(downloadedSize)}/${formatFileSize(totalSize)}, 分片: ${downloadedSegments}/${totalSegments}`);
                }
            }, 100);
        }
        
        function pauseSimulation() {
            isPaused = true;
            log('下载已暂停');
            updateDisplay();
        }
        
        function resumeSimulation() {
            isPaused = false;
            log('下载已继续');
        }
        
        function resetSimulation() {
            if (simulationInterval) {
                clearInterval(simulationInterval);
            }
            currentProgress = 0;
            downloadedSize = 0;
            downloadedSegments = 0;
            isPaused = false;
            updateDisplay();
            log('模拟已重置');
        }
        
        function clearLog() {
            document.getElementById('log').innerHTML = '';
        }
        
        // 初始化显示
        updateDisplay();
        log('进度显示测试页面已加载');
    </script>
</body>
</html>
